package com.hupu.jrs.ui.account

import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.facebook.drawee.view.SimpleDraweeView
import com.hupu.jrs.R
import com.hupu.jrs.db.User
import java.util.*
import javax.inject.Inject

/**
 * Created by sll on 2016/3/10.
 */
class AccountAdapter @Inject
constructor() : RecyclerView.Adapter<AccountAdapter.ViewHolder>() {

    private var users: List<User> = ArrayList<User>()
    private var onItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onAccountItemDelClicked(user: User?)

        fun onAccountItemClicked(user: User?)
    }

    fun bind(users: List<User>) {
        this.users = users
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context)
                .inflate(R.layout.item_list_account, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val user = users[position]
        holder.user = user
        if (!TextUtils.isEmpty(user.icon)) {
            holder.ivIcon!!.setImageURI(Uri.parse(user.icon))
        }
        holder.tvName!!.setText(user.userName)
        holder.tvDesc!!.setText(user.userName)
    }

    override fun getItemCount(): Int {
        return users.size
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        @BindView(R.id.tvName)
        var tvName: TextView? = null
        @BindView(R.id.tvDesc)
        var tvDesc: TextView? = null
        @BindView(R.id.ivIcon)
        var ivIcon: SimpleDraweeView? = null

        var user: User? = null

        @OnClick(R.id.rlDel)
        fun rlDelClick() {
            if (onItemClickListener != null) {
                onItemClickListener!!.onAccountItemDelClicked(user)
            }
        }

        @OnClick(R.id.llItem)
        fun llItemClick() {
            if (onItemClickListener != null) {
                onItemClickListener!!.onAccountItemClicked(user)
            }
        }

        init {
            ButterKnife.bind(this, itemView)
        }
    }
}
